package demo;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class TestBinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left; // 左孩子
        public TreeNode right; //右孩子

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root; //二叉树的根节点
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        //TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;

        return A;

    }
    //前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    //后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    /**
     * @param root
     * @return void
     * @description  非递归的前序遍历
     */
    public void preOrderNor(TreeNode root){
        if(root == null){
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new LinkedList<>();
        //cur不为空
        //栈不为空
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    public void inOrderNor(TreeNode root){
        if(root == null){
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new LinkedList<>();
        //cur不为空
        //栈不为空
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    public void postOrderNor(TreeNode root){
        if(root == null){
            return;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        Deque<TreeNode> stack = new LinkedList<>();
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev){
                System.out.print(top.val + " ");
                stack.pop();
                prev = top;
            }else{
                cur = top.right;
            }
        }
    }




    // 获取树中节点的个数
    //左树节点+右树节点+1
    /*
    * 时间复杂度：O(N)
    * 空间复杂度：O(logN)  log以2为低的n
    * 递归在栈上开辟内存，开辟的内存就是 树的高度
    * */
    public int size(TreeNode root){
        if(root == null){
            return 0;
        }
        int countLeft = size(root.left);
        int countRight = size(root.right);
        return countLeft+countRight+1;
    }

    public static int nodeSize = 0; // 二叉树的总结点
    public void  size2(TreeNode root){
        if(root == null){
            return ;
        }
        nodeSize++; // 根不为空，就++
        size2(root.left);
        size2(root.right);
    }


   // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        int countLeft = getLeafNodeCount(root.left);
        int countRight = getLeafNodeCount(root.right);
        return countLeft+countRight;
    }


    public static int leafNode = 0;  //叶子节点
    public void getLeafNodeCount2(TreeNode root) {
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            leafNode++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }



    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        int leftSize = getKLevelNodeCount(root.left,k-1);
        int rightSize = getKLevelNodeCount(root.right,k-1);
        return leftSize+rightSize;
    }


    // 获取二叉树的高度
    /*
    * 时间复杂度：O(N)
    * 空间复杂度：O(树的高度)
    * */
    public  int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return (leftHight > rightHight ? leftHight : rightHight)+1;
    }


    // 检测值为value的元素是否存在
    /*
    * 1. root为空，返回空
    * 2. root.val  == val  返回该节点
    * */
    public TreeNode find(TreeNode root, int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode leftFind = find(root.left,val);
        if(leftFind != null){
            return leftFind;
        }
        TreeNode rightFind = find(root.right,val);
        if(rightFind != null){
            return rightFind;
        }
        return null; // 没有找到
    }


    //层序遍历
    public  void levelOrder(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+ " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }

    }
    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> ret = new LinkedList<>();
        if(root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Character> list = new LinkedList<>();
            int size = queue.size();
            while(size-- != 0){
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
            System.out.print(list.get(0) + " ");
            ret.add(list);
        }
        return ret;
    }


public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    //1. 两个栈当中 存储好数据
    Deque<TreeNode> stack1 = new LinkedList<>();
    getPath(root,p,stack1);
    Deque<TreeNode> stack2 = new LinkedList<>();
    getPath(root,q,stack2);
    //2. 判断栈的大小
    int size1 = stack1.size();
    int size2 = stack2.size();
    if(size1 > size2){
        int size = size1-size2;
        while(size-- != 0){
            stack1.pop();
        }
    }else{
        int size = size2-size1;
        while(size-- != 0){
            stack2.pop();
        }
    }
    //栈里面的数据个数是一样,而且都不为空
    while(!stack1.isEmpty() && !stack2.isEmpty()){
        if(stack1.peek() != stack2.peek()){//不相同
            stack1.pop();
            stack2.pop();
        }else{//相同
            return stack1.peek();
        }
    }
    return null;
}

    public boolean getPath(TreeNode root, TreeNode node, Deque<TreeNode> stack){
        if(root == null || node  == null) {
            return false;
        }
        stack.push(root);//放完之后，要检查
        if(root == node){
            return true;
        }
        boolean leftRet = getPath(root.left,node,stack);
        if(leftRet){
            return true;
        }
        boolean rightRet = getPath(root.right,node,stack);
        if(rightRet){
            return true;
        }
        stack.pop();
        return false;
    }

    // 判断一棵树是不是完全二叉树
    public  boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur == null){
               break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }

        while (!queue.isEmpty()){
            TreeNode tmp = queue.poll();
            if(tmp != null){
                return false;
            }
        }
        return true;
    }



}
